#include "menseimainwindow.h"

#include <QDebug>
#include "core/constants.h"
#include "core/customercontroller.h"
#include "gui/customerview.h"

void MenseiMainWindow::onFileOpened(QString filePath)
{
    qDebug() << "MenseiMainWindow: " << filePath;

	custCtrl->getAll();
	itemCtrl->getAll();
	transCtrl->getAll();
	supplierCtrl->getAll();

    this->enableTabs();
}

/*
  Creates the controllers and views, and connects them together.
 */
void MenseiMainWindow::createControllersAndViews()
{
    homeScreen = new HomeScreen;

    custCtrl = new CustomerController;
    custView = new CustomerView;
    custView->setController(custCtrl);

    itemCtrl = new ItemController;
    itemView = new ItemView;
    itemView->setController(itemCtrl);

    transCtrl = new TransactionController;
    transView = new TransactionView;
    transView->setController(transCtrl);

    supplierCtrl = new SupplierController;
    supplierView = new SupplierView;
    supplierView->setController(supplierCtrl);
}

/*
  Disables all tabs, except for the Home tab. Use this
  when no file is open, as the other tabs will have no purpose
  until a file is opened.
 */
void MenseiMainWindow::disableTabs()
{
    int i;
    for (i = 1; i < tabWidget->count(); i++) {
        tabWidget->setTabEnabled(i, false);
    }
}

/*
  Enables all tabs. Use this when a file is opened.
 */
void MenseiMainWindow::enableTabs()
{
    int i;
    for (i = 1; i < tabWidget->count(); i++) {
        tabWidget->setTabEnabled(i, true);
    }
	custCtrl->getAll();
}

/*
  Initializes the main window's UI.
 */
void MenseiMainWindow::initWndComponents()
{
    QWidget *container = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout;

    this->createControllersAndViews();

    tabWidget = new QTabWidget;
    tabWidget->addTab(this->homeScreen, "Home");
    tabWidget->addTab(this->transView, "Transactions");
    tabWidget->addTab(this->itemView, "Inventory");
    tabWidget->addTab(this->supplierView, "Suppliers");
    tabWidget->addTab(this->custView, "Customers");

    layout->addWidget(tabWidget);

    container->setLayout(layout);
    this->setCentralWidget(container);
}

void MenseiMainWindow::initWndSignalsAndSlots()
{
    connect(this->homeScreen, SIGNAL(fileOpened(QString)),
            this, SLOT(onFileOpened(QString)));
}

MenseiMainWindow::MenseiMainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    tabCount = 0;

    this->setWindowTitle(APPNAME);
    this->setMinimumSize(640, 480);
    this->initWndComponents();
    this->initWndSignalsAndSlots();

    this->disableTabs();
}
